package api

import (
	"api-getaway/basic/config"
	"api-getaway/handler/request"
	"api-getaway/model"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func BeautyList(r *gin.Context) {
	var req request.Beauty
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	var all []model.Beauty

	key := fmt.Sprintf("beauty_list_")

	get := config.Rdb.Get(config.Ctx, key).Val()
	if get != "" {
		fmt.Println("使用缓存")
		json.Unmarshal([]byte(key), &all)
	} else {
		marshal, _ := json.Marshal(all)
		config.Rdb.Set(config.Ctx, key, marshal, time.Minute*3)
	}

	config.DB.Debug().Find(&all)
	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "美容套餐列表展示成功",
		"data":    all,
	})
}

func BeautyShow(r *gin.Context) {
	var req request.Beauty
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	var all model.Beauty
	key := fmt.Sprintf("beauty_list_")

	get := config.Rdb.Get(config.Ctx, key).Val()
	if get != "" {
		fmt.Println("使用缓存")
		json.Unmarshal([]byte(key), &all)
	} else {
		marshal, _ := json.Marshal(all)
		config.Rdb.Set(config.Ctx, key, marshal, time.Minute*3)
	}
	config.DB.Debug().First(&all)
	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "美容套餐详情展示成功",
		"data":    all,
	})
}
